package chess

import (
	"gddgame.cc/galaxy/asteroid/cluster"
	"gddgame.cc/galaxy/common/feature/orbit"
	"gddgame.cc/galaxy/utils"
)

type ClientBlockDelegate interface {
	GetWithSub(container cluster.Container, mapId string, sub string, level orbit.Point) *cluster.Client
	GetSub(size orbit.Location, position []byte, log bool) string
}

type ClientComponent struct {
	cluster.Node

	Block      *orbit.BlockLocation
	MapId      string
	levelPoint orbit.Point
	level      uint

	service ClientBlockDelegate
}

func (block *ClientComponent) GetClient(position []byte) *cluster.Client {
	sub := block.service.GetSub(block.Block.Gap, position, false)
	return block.service.GetWithSub(block.Node.Container(), block.MapId, sub, block.levelPoint)
}

func (block *ClientComponent) GetSub(position []byte, log bool) string {
	return block.service.GetSub(block.Block.Gap, position, log)
}

func (block *ClientComponent) GetSubWithLevel(position []byte, level orbit.Point, log bool) string {
	return block.service.GetSub(block.Block.Gap.Multiple(level), position, log)
}

func (block *ClientComponent) GetClientWithSub(sub string) *cluster.Client {
	return block.service.GetWithSub(block.Node.Container(), block.MapId, sub, block.levelPoint)
}

func (block *ClientComponent) GetClientWithSubLevel(sub string, level orbit.Point) *cluster.Client {
	return block.service.GetWithSub(block.Node.Container(), block.MapId, sub, level)
}

func (block *ClientComponent) GetBlock() *orbit.BlockLocation {
	return block.Block
}

func (block *ClientComponent) GetMap() string {
	return block.MapId
}

func (block *ClientComponent) CanSend(r uint16) bool {
	return r == 0 || utils.IncludeInt(int(r), block.level)
}
